/**
 * 
 */
package com.scpii.universal.service.mobile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.scpii.universal.hibernate.entity.ActiveUserTab;
import com.scpii.universal.hibernate.entity.AppIconTab;
import com.scpii.universal.hibernate.entity.AppLoadingUpdateTab;
import com.scpii.universal.hibernate.entity.AppPageViewTab;
import com.scpii.universal.hibernate.entity.ClientUserTab;
import com.scpii.universal.hibernate.entity.CommentTab;
import com.scpii.universal.hibernate.entity.DataInterfaceUpdateTab;
import com.scpii.universal.hibernate.entity.FavoritesTab;
import com.scpii.universal.hibernate.entity.FeedbackTab;
import com.scpii.universal.hibernate.entity.ManageUserTab;
import com.scpii.universal.hibernate.entity.MoreContentTab;
import com.scpii.universal.hibernate.entity.OrderTab;
import com.scpii.universal.hibernate.entity.PushMessageTab;
import com.scpii.universal.hibernate.entity.ShareTab;
import com.scpii.universal.pb.PbUniversal.PBRESULT;
import com.scpii.universal.pb.PbUniversal.PbAddCommentCS;
import com.scpii.universal.pb.PbUniversal.PbAddCommentSC;
import com.scpii.universal.pb.PbUniversal.PbAddFavoritesCS;
import com.scpii.universal.pb.PbUniversal.PbAddFavoritesSC;
import com.scpii.universal.pb.PbUniversal.PbAddFeedbackCS;
import com.scpii.universal.pb.PbUniversal.PbAddFeedbackSC;
import com.scpii.universal.pb.PbUniversal.PbAddShareCS;
import com.scpii.universal.pb.PbUniversal.PbAddShareSC;
import com.scpii.universal.pb.PbUniversal.PbCommentListCS;
import com.scpii.universal.pb.PbUniversal.PbCommentListSC;
import com.scpii.universal.pb.PbUniversal.PbDataUpdateCS;
import com.scpii.universal.pb.PbUniversal.PbDataUpdateSC;
import com.scpii.universal.pb.PbUniversal.PbFavoritesListCS;
import com.scpii.universal.pb.PbUniversal.PbFavoritesListSC;
import com.scpii.universal.pb.PbUniversal.PbFeedbackListCS;
import com.scpii.universal.pb.PbUniversal.PbFeedbackListSC;
import com.scpii.universal.pb.PbUniversal.PbJoinActiveCS;
import com.scpii.universal.pb.PbUniversal.PbJoinActiveSC;
import com.scpii.universal.pb.PbUniversal.PbMoreContentListCS;
import com.scpii.universal.pb.PbUniversal.PbMoreContentListSC;
import com.scpii.universal.pb.PbUniversal.PbOrderCS;
import com.scpii.universal.pb.PbUniversal.PbOrderListCS;
import com.scpii.universal.pb.PbUniversal.PbOrderListSC;
import com.scpii.universal.pb.PbUniversal.PbOrderSC;
import com.scpii.universal.pb.PbUniversal.PbPushMessageListCS;
import com.scpii.universal.pb.PbUniversal.PbPushMessageListSC;
import com.scpii.universal.pb.PbUniversal.PbUpdateAppLoadingImgCS;
import com.scpii.universal.pb.PbUniversal.PbUpdateAppLoadingImgSC;
import com.scpii.universal.pb.PbUniversalBasic.PbAppLoadingImg;
import com.scpii.universal.pb.PbUniversalBasic.PbAppLoadingImgList;
import com.scpii.universal.pb.PbUniversalBasic.PbComment;
import com.scpii.universal.pb.PbUniversalBasic.PbCommentList;
import com.scpii.universal.pb.PbUniversalBasic.PbFavorites;
import com.scpii.universal.pb.PbUniversalBasic.PbFavoritesList;
import com.scpii.universal.pb.PbUniversalBasic.PbFeedback;
import com.scpii.universal.pb.PbUniversalBasic.PbFeedbackList;
import com.scpii.universal.pb.PbUniversalBasic.PbInterfaceUpdate;
import com.scpii.universal.pb.PbUniversalBasic.PbInterfaceUpdateList;
import com.scpii.universal.pb.PbUniversalBasic.PbMoreContent;
import com.scpii.universal.pb.PbUniversalBasic.PbMoreContentList;
import com.scpii.universal.pb.PbUniversalBasic.PbOrder;
import com.scpii.universal.pb.PbUniversalBasic.PbOrderList;
import com.scpii.universal.pb.PbUniversalBasic.PbPushMessage;
import com.scpii.universal.pb.PbUniversalBasic.PbPushMessageList;
import com.scpii.universal.pb.PbUniversalBasic.PbShare;
import com.scpii.universal.service.ActiveUserTabService;
import com.scpii.universal.service.AppIconTabService;
import com.scpii.universal.service.AppLoadingUpdateTabService;
import com.scpii.universal.service.AppPageViewTabService;
import com.scpii.universal.service.ClientUserTabService;
import com.scpii.universal.service.CommentTabService;
import com.scpii.universal.service.DataInterfaceUpdateTabService;
import com.scpii.universal.service.FavoritesTabService;
import com.scpii.universal.service.FeedbackTabService;
import com.scpii.universal.service.ManageUserTabService;
import com.scpii.universal.service.MoreContentTabService;
import com.scpii.universal.service.OrderTabService;
import com.scpii.universal.service.PushMessageTabService;
import com.scpii.universal.service.ShareTabService;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.DateAndString;

/**
 * UniversalApi
 * 
 * @author 甘建新 2012-7-11下午11:02:49
 */
public class MobileFunctionService {
//	private FeedbackTabService feedbackTabService = new FeedbackTabService();
//	private ClientUserTabService clientUserTabService = new ClientUserTabService();
//	private FavoritesTabService favoritesTabService = new FavoritesTabService();
//	private ShareTabService shareTabService = new ShareTabService();
//	private CommentTabService commentTabService = new CommentTabService();
//	private OrderTabService orderTabService = new OrderTabService();
//	private MoreContentTabService moreContentTabService = new MoreContentTabService();
//	private PushMessageTabService pushMessageTabService = new PushMessageTabService();
//	private DataInterfaceUpdateTabService dataInterfaceUpdateTabService = new DataInterfaceUpdateTabService();
//	private AppLoadingUpdateTabService appLoadingUpdateTabService = new AppLoadingUpdateTabService();
//	private AppIconTabService appIconTabService = new AppIconTabService();
	
	private FeedbackTabService feedbackTabService;
	private ClientUserTabService clientUserTabService;
	private FavoritesTabService favoritesTabService;
	private ShareTabService shareTabService;
	private CommentTabService commentTabService;
	private OrderTabService orderTabService;
	private MoreContentTabService moreContentTabService;
	private PushMessageTabService pushMessageTabService;
	private DataInterfaceUpdateTabService dataInterfaceUpdateTabService;
	private AppLoadingUpdateTabService appLoadingUpdateTabService;
	private AppIconTabService appIconTabService;
	private ActiveUserTabService activeUserTabService;
	private AppPageViewTabService appPageViewTabService;
	private ManageUserTabService manageUserTabService;
	
	private Logger log = Logger.getLogger(MobileFunctionService.class);
	private String LOG_METHOD_NAME="";
	/**
	 * 获得留言反馈列表 GET_FEEDBACKLIST
	 * 
	 * @param feedbackCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:25:56
	 */
	public byte[] getFeedbackList(byte[] bytes) {
		LOG_METHOD_NAME="getFeedbackList";
		
		PbFeedbackListSC.Builder pbFbListSCB = PbFeedbackListSC.newBuilder();
		PbFeedbackList.Builder pbFbListB = PbFeedbackList.newBuilder();
		if (bytes != null) {
			try {
				//转换参数
				PbFeedbackListCS pbFbListCS = PbFeedbackListCS.parseFrom(bytes);
				int appId = pbFbListCS.getIntAppId();
				int feedbackType = pbFbListCS.getIntFeedbackType();
				int pageNum = pbFbListCS.getIntPageNum();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",userId:"+pbFbListCS.getIntUserId()+",feedbackType:"+feedbackType+",pageNum:"+pageNum+"】");
				Long userId = StringUtils.isBlank(pbFbListCS.getIntUserId())?0l:Long.parseLong(String.valueOf(pbFbListCS.getIntUserId()));
				
				int pageCount = feedbackTabService.getFeedBackListPageCount(appId, userId, feedbackType);
				pbFbListSCB.setIntPageCount(pageCount);
				//查询数据
				List<FeedbackTab> fbList = feedbackTabService.getFeedbackListByPageNo(appId, userId, feedbackType, pageNum);
				if (fbList!=null && fbList.size()>0) {
					for (FeedbackTab fb : fbList) {
						PbFeedback.Builder pbFbBuilder = PbFeedback.newBuilder();
//						pbFbBuilder.setIntAppId(fb.getIntAppId());
//						pbFbBuilder.setIntFeedbackId(fb.getIntFeedbackId());
//						pbFbBuilder.setIntFeedbackType(fb.getIntFeedbackType());
//						//回复留言对象ID
//						pbFbBuilder.setIntParentFeedbackId(fb.getIntParentFeedbackId());
//						pbFbBuilder.setIntUserId(fb.getIntUserId());
//						//获得留言反馈用的用户名
//						pbFbBuilder.setVarUserName(clientUserTabService.get(fb.getIntUserId()).getVarNickName());
//						pbFbBuilder.setVarFeedbackTitle(fb.getVarFeedbackTitle());
//						pbFbBuilder.setVarFeedbackContent(fb.getVarFeedbackContent());
						
						pbFbBuilder = this.setPbFeedback(fb,Const.FEEDBACK_USER_TYPE_USER);
						
						PbFeedbackList.Builder pbFbListB2 = PbFeedbackList.newBuilder();
						List<FeedbackTab> feedbackTabList = fb.getFeedbackList();
						if (feedbackTabList!=null && feedbackTabList.size()>0) {
							for (FeedbackTab feedbackTab : feedbackTabList) {
								PbFeedback.Builder pbFbBuilder2 = PbFeedback.newBuilder();
								pbFbBuilder2 = this.setPbFeedback(feedbackTab, Const.FEEDBACK_USER_TYPE_ADMIN);
								pbFbListB2.addPbFeedbackList(pbFbBuilder2);
							}
						}
						pbFbBuilder.setPbFeedbackList(pbFbListB2);
						//加到List中
						pbFbListB.addPbFeedbackList(pbFbBuilder);
					}
					pbFbListSCB.setPbFeedbackList(pbFbListB);
					//设置数据获取状态——成功
					pbFbListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					//设置数据获取状态——数据为空
					pbFbListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				//设置数据获取状态——数据获取异常
				pbFbListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		} else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			////设置数据获取状态——失败
			pbFbListSCB.setExeResult(PBRESULT.FAILED);
		}
		pbFbListSCB.setPbFeedbackList(pbFbListB);
		return pbFbListSCB.build().toByteArray();
	}
	
	/**
	 * 填充留言反馈
	 * @param fb
	 * @param userType:0:管理员、1：客户端用户
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-9-25下午04:55:07
	 */
	private PbFeedback.Builder setPbFeedback(FeedbackTab fb,int userType)throws Exception{
		LOG_METHOD_NAME="PbFeedback";
		log.info(LOG_METHOD_NAME+"【FeedbackTab:"+fb.toString()+",userType:"+userType+"】");
		PbFeedback.Builder pbFbBuilder = PbFeedback.newBuilder();
		if (fb!=null) {
			pbFbBuilder.setIntAppId(fb.getIntAppId());
			pbFbBuilder.setIntFeedbackId(fb.getIntFeedbackId());
			pbFbBuilder.setIntFeedbackType(fb.getIntFeedbackType());
			//回复留言对象ID
			pbFbBuilder.setIntParentFeedbackId(fb.getIntParentFeedbackId());

			pbFbBuilder.setVarFeedbackTitle(fb.getVarFeedbackTitle());
			pbFbBuilder.setVarFeedbackContent(fb.getVarFeedbackContent());
			
			Long userId = 0l;
			String userName="";
			if (userType==Const.FEEDBACK_USER_TYPE_ADMIN) {
				userId = (long) fb.getIntAdminId();
				ManageUserTab manageUserTab = manageUserTabService.get(fb.getIntAdminId());
				if (manageUserTab!=null) {
					userName = manageUserTab.getVarLoginName();
				}
			}else if(userType==Const.FEEDBACK_USER_TYPE_USER) {
				userId = fb.getIntUserId();
				ClientUserTab clientUserTab = clientUserTabService.get(fb.getIntUserId());
				if (clientUserTab!=null) {
					userName = clientUserTab.getVarNickName();
				}
			}
			//获得留言反馈用的用户名
			pbFbBuilder.setIntUserId(userId.toString());
			pbFbBuilder.setVarUserName(userName);
		}
		return pbFbBuilder;
	}

	/**
	 * 添加留言反馈 ADD_FEEDBACK
	 * 
	 * @param addFeedbackCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:27:22
	 */
	public byte[] addFeedback(byte[] bytes) {
		LOG_METHOD_NAME="addFeedback";
		PbAddFeedbackSC.Builder pbAddFbSCB = PbAddFeedbackSC.newBuilder();
		if (bytes!=null) {
			try {
				//将字节流转成PB对象
				PbAddFeedbackCS pbAddFbCS = PbAddFeedbackCS.parseFrom(bytes);
				//获得PB对象中包含的子PB对象
				PbFeedback pbFb = pbAddFbCS.getPbFeedback();
				
				log.info(LOG_METHOD_NAME+"【PbFeedback:"+pbAddFbCS.toString()+"】");
				
				//实例化一个FeedbackTab实体类
				FeedbackTab fb = new FeedbackTab();
				
				//将PB对象中的数据转到实体中
				fb.setIntAppId(pbFb.getIntAppId());
				fb.setIntDeleteSign(0);
				fb.setIntFeedbackType(pbFb.getIntFeedbackType());
				fb.setIntParentFeedbackId(pbFb.getIntParentFeedbackId());
				Long userId = StringUtils.isBlank(pbFb.getIntUserId())?0l:Long.parseLong(String.valueOf(pbFb.getIntUserId()));
				fb.setIntUserId(userId);
				
				fb.setVarFeedbackContent(pbFb.getVarFeedbackContent());
				fb.setVarFeedbackTitle(pbFb.getVarFeedbackTitle());
				
				fb.setDatFeedbackDatetime(DateAndString.dateToString(new Date()));
				fb.setDatReplyDatetime(DateAndString.dateToString(new Date()));
				fb.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
				fb.setIntReplyStatus(0);
				
				feedbackTabService.save(fb);
				//设置数据操作结果状态——成功
				pbAddFbSCB.setExeResult(PBRESULT.SUCCESS);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				//设置数据操作结果状态——异常
				pbAddFbSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			//设置数据操作结果状态——失败
			pbAddFbSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAddFbSCB.build().toByteArray();
	}

	/**
	 * 获得收藏列表 GET_FAVORITESLIST
	 * 
	 * @param favoritesListCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:30:29
	 */
	public byte[] getFavoritesList(byte[] bytes) {
		LOG_METHOD_NAME="getFavoritesList";
		
		PbFavoritesListSC.Builder pbFListSCB = PbFavoritesListSC.newBuilder();
		PbFavoritesList.Builder pbFListB = PbFavoritesList.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbFavoritesListCS pbFListCS = PbFavoritesListCS.parseFrom(bytes);
				int appId = pbFListCS.getIntAppId();
				int userId = pbFListCS.getIntUserId();
				int pageNum = pbFListCS.getIntPageNum();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",userId:"+userId+",pageNum:"+pageNum+"】");
				
				int pageCount = favoritesTabService.getFavoritesPageCount(appId, userId);
				
				pbFListSCB.setIntPageCount(pageCount);
				//执行查询
				List<FavoritesTab> favList = favoritesTabService.getFavoritesListByPageNo(appId, userId, pageNum);
				if (favList!=null && favList.size()>0) {
					for (FavoritesTab fav : favList) {
						PbFavorites.Builder pbFav = PbFavorites.newBuilder();
						pbFav.setIntAppId(fav.getIntAppId());
						pbFav.setIntContentId(fav.getIntContentId());
						pbFav.setIntFavoritesId(fav.getIntFavoriteId());
						pbFav.setIntType(fav.getIntType());
						pbFav.setIntUserId(fav.getIntUserId());
						
						pbFListB.addPbFavoritesList(pbFav);
					}
					pbFListSCB.setPbFavoritesList(pbFListB);
					pbFListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbFListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbFListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbFListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbFListSCB.build().toByteArray();
	}

	/**
	 * 添加收藏 ADD_FAVORITES
	 * 
	 * @param favoritesCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:38:21
	 */
	public byte[] addFavorites(byte[] bytes) {
		LOG_METHOD_NAME="addFavorites";
		
		PbAddFavoritesSC.Builder pbAddFSCB = PbAddFavoritesSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbAddFavoritesCS pbAddFCS = PbAddFavoritesCS.parseFrom(bytes);
				
				log.info(LOG_METHOD_NAME+"【PbAddFavoritesCS:"+pbAddFCS.toString()+"】");
				
				//获得其中的元素
				PbFavorites pbF = pbAddFCS.getPbFavorites();
				//将参数转换到实体类中
				boolean bool = favoritesTabService.checkSameFavoritesIn(pbF.getIntUserId(), pbF.getIntContentId());
				if (bool) {
					FavoritesTab ftab = new FavoritesTab();
					ftab.setIntAppId(pbF.getIntAppId());
					ftab.setIntContentId(pbF.getIntContentId());
					ftab.setIntDeleteSign(0);
					ftab.setIntType(pbF.getIntType());
					ftab.setIntUserId(pbF.getIntUserId());
					
					ftab.setDatFavoriteDatetime(DateAndString.dateToString(new Date()));
					ftab.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
					
					//存储数据
					favoritesTabService.save(ftab);
					//设置数据结果
					pbAddFSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+"：重复收藏");
					pbAddFSCB.setExeResult(PBRESULT.OTHER);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbAddFSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else{
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbAddFSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAddFSCB.build().toByteArray();
	}

	/**
	 * 添加分享 ADD_SHARE
	 * 
	 * @param addShareCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:39:44
	 */
	public byte[] addShare(byte[] bytes) {
		LOG_METHOD_NAME="addShare";
		
		PbAddShareSC.Builder pbAddShareSCB = PbAddShareSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbAddShareCS pbAddShareCS = PbAddShareCS.parseFrom(bytes);
				PbShare pbShare = pbAddShareCS.getPbShare();
				
				log.info(LOG_METHOD_NAME+"【PbAddShareCS:"+pbAddShareCS.toString()+"】");
				
				ShareTab shareTab = new ShareTab();
				//设置实体数据
				shareTab.setIntAppId(pbShare.getIntAppId());
				shareTab.setIntContentId(pbShare.getIntContentId());
				shareTab.setIntDeleteSign(0);
				shareTab.setIntShareType(pbShare.getIntShareType());
				shareTab.setIntUserId(pbShare.getIntUserId());
				
				shareTab.setVarShareContent(pbShare.getVarShareContent());
				
				shareTab.setDatShareDaetime(DateAndString.dateToString(new Date()));
				shareTab.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
				
				//保存数据 
				shareTabService.save(shareTab);
				
				//设置数据操作结果状态
				pbAddShareSCB.setExeResult(PBRESULT.SUCCESS);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				//设置数据操作结果状态
				pbAddShareSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		} else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			//设置数据操作结果状态
			pbAddShareSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAddShareSCB.build().toByteArray();
	}

	/**
	 * 获得内容评论列表 GET_COMMENTLIST
	 * 
	 * @param commentListCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:40:52
	 */
	public byte[] getCommentList(byte[] bytes) {
		LOG_METHOD_NAME="getCommentList";
		
		PbCommentListSC.Builder pbComListSCB = PbCommentListSC.newBuilder();
		PbCommentList.Builder pbComListB = PbCommentList.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbCommentListCS pbComListCS = PbCommentListCS.parseFrom(bytes);
				int appId = pbComListCS.getIntAppId();
				int contentIds = pbComListCS.getIntContentId();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",contentIds:"+contentIds+"】");
				
				// 查询数据
				List<CommentTab> commList = commentTabService.getCommentListByContentId(contentIds);
				if (commList!=null && commList.size()>0) {
					for (CommentTab comm : commList) {
						PbComment.Builder pbCommB = PbComment.newBuilder();
						pbCommB.setIntCommentId(comm.getIntCommentId());
						pbCommB.setIntContentId(comm.getIntContentId());
						pbCommB.setIntUserId(comm.getIntUserId());
						pbCommB.setVarCommentName(comm.getVarCommentName());
						pbCommB.setVarCommentContent(comm.getVarCommentContent());
						pbCommB.setDatCommentDatetime(comm.getDatCommentDatetime());
						
						pbComListB.addPbCommentList(pbCommB);
					}
					pbComListSCB.setPbCommentList(pbComListB);
					//执行结果状态
					pbComListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {//执行结果状态
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbComListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				//执行结果状态
				pbComListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			//执行结果状态
			pbComListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbComListSCB.build().toByteArray();
	}

	/**
	 * 添加内容评论 ADD_COMMENT
	 * 
	 * @param addCommentCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:42:00
	 */
	public byte[] addComment(byte[] bytes) {
		LOG_METHOD_NAME="addComment";
		
		PbAddCommentSC.Builder pbAddCommSCB = PbAddCommentSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbAddCommentCS pbAddComCS = PbAddCommentCS.parseFrom(bytes);
				
				log.info(LOG_METHOD_NAME+"【PbAddCommentCS:"+pbAddComCS.toString()+"】");
				
				PbComment pbComm = pbAddComCS.getPbComment();
				//参数设置到实体在中
				CommentTab comm = new CommentTab();
				comm.setIntContentId(pbComm.getIntContentId());
				comm.setIntUserId(pbComm.getIntUserId());
				comm.setIntDeleteSign(0);
				
				comm.setVarCommentName(pbComm.getVarCommentName());
				comm.setVarCommentContent(pbComm.getVarCommentContent());
				
				comm.setDatCommentDatetime(DateAndString.dateToString(new Date()));
				comm.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
				
				//保存数据
				commentTabService.save(comm);
				//设置数据操作状态
				pbAddCommSCB.setExeResult(PBRESULT.SUCCESS);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				//设置数据操作状态
				pbAddCommSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else{
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			//设置数据操作状态
			pbAddCommSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAddCommSCB.build().toByteArray();
	}

	/**
	 * 获得用户订单列表 GET_ORDERLIST
	 * 
	 * @param orderListCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:43:46
	 */
	public byte[] getOrderList(byte[] bytes) {
		LOG_METHOD_NAME="getOrderList";
		
		PbOrderListSC.Builder pbOrderListSCB = PbOrderListSC.newBuilder();
		PbOrderList.Builder pbOrderListB = PbOrderList.newBuilder();
		if (bytes!=null) {
			try {
				PbOrderListCS pbOrderListCS = PbOrderListCS.parseFrom(bytes);
				int appId = pbOrderListCS.getIntAppId();
				int userId = pbOrderListCS.getIntUserId();
				int pageNum = pbOrderListCS.getIntPageNum();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",userId:"+userId+",pageNum:"+pageNum+"】");
				
				//执行查询
				int pageCount = orderTabService.getOrderListPageCount(userId, appId);
				pbOrderListSCB.setIntPageCount(pageCount);
				
				List<OrderTab> orderList = orderTabService.getOrderListByPageNo(userId, appId, pageNum);
				if (orderList!=null && orderList.size()>0) {
					for (OrderTab order : orderList) {
						PbOrder.Builder pbOrderB = PbOrder.newBuilder();
						pbOrderB.setIntAppId(order.getIntAppId());
						pbOrderB.setIntConentId(order.getIntContentId());
						pbOrderB.setIntOrderCount(order.getIntOrderCount());
						pbOrderB.setIntOrderId(order.getIntOrderId());
						pbOrderB.setIntOrderStatus(order.getIntOrderStatues());
						pbOrderB.setIntUserId(order.getIntUserId());
						
						pbOrderB.setDobOrderMoney(order.getDobOrderMoney());
						pbOrderB.setDatOrderDatetime(order.getDatOrderDatetime());
						
						pbOrderListB.addPbOrder(pbOrderB);
					}
					pbOrderListSCB.setPbOrderList(pbOrderListB);
					pbOrderListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbOrderListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbOrderListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbOrderListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbOrderListSCB.build().toByteArray();
	}

	/**
	 * 新增订单 ADD_ORDER
	 * 
	 * @param orderCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:45:20
	 */
	public byte[] addOrder(byte[] bytes) {
		LOG_METHOD_NAME="addOrder";
		
		PbOrderSC.Builder pbOrderSCB = PbOrderSC.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbOrderCS pbOrderCS = PbOrderCS.parseFrom(bytes);
				PbOrderList pbOrderList = pbOrderCS.getPbOrderList();
				
				log.info(LOG_METHOD_NAME+"【PbOrderCS:"+pbOrderCS.toString()+"】");
				
				List<PbOrder> pbOrderL = pbOrderList.getPbOrderList();
				//设置实体数据，并保存对应数据
				if (pbOrderL!=null && pbOrderL.size()>0) {
					for (PbOrder pbOrder : pbOrderL) {
						OrderTab order = new OrderTab();
						//设置数据
						order.setIntAppId(pbOrder.getIntAppId());
						order.setIntContentId(pbOrder.getIntConentId());
						order.setIntDeleteSign(0);
						order.setIntOrderCount(pbOrder.getIntOrderCount());
						order.setIntOrderStatues(pbOrder.getIntOrderStatus());
						order.setIntUserId(pbOrder.getIntUserId());
						
						order.setDobOrderMoney(pbOrder.getDobOrderMoney());
						order.setDatOrderDatetime(pbOrder.getDatOrderDatetime());
						order.setDatUpdateDatetime(DateAndString.dateToString(new Date()));
						
						//保存数据
						orderTabService.save(order);
					}
				}
				//设置数据操作状态
				pbOrderSCB.setExeResult(PBRESULT.SUCCESS);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbOrderSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbOrderSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbOrderSCB.build().toByteArray();
	}

	/**
	 * 获得更多内容列表 GET_MORE_CONTENTLIST
	 * 
	 * @param moreContentListCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:46:43
	 */
	public byte[] getMoreContentList(byte[] bytes) {
		LOG_METHOD_NAME="getMoreContentList";
		
		PbMoreContentListSC.Builder pbMoreListSCB = PbMoreContentListSC.newBuilder();
		PbMoreContentList.Builder pbMoreListB = PbMoreContentList.newBuilder();
		if (bytes!=null) {
			try {
				PbMoreContentListCS pbMoreListCS = PbMoreContentListCS.parseFrom(bytes);
				int appId = pbMoreListCS.getIntAppId();
				int pageNum = pbMoreListCS.getIntPageNum();
				int pageId = pbMoreListCS.getIntPageId();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",pageNum:"+pageNum+",pageId:"+pageId+"】");
				
//				int pageCount = moreContentTabService.getMoreContentPageCount(appId,pageId);
				pbMoreListSCB.setIntPageCount(0);
				//查询数据
				List<MoreContentTab> moreContList = moreContentTabService.getMoreContentListByPage(appId,pageId, pageNum);
				if (moreContList!=null && moreContList.size()>0) {
					for (MoreContentTab more : moreContList) {
						PbMoreContent.Builder pbMoreB = PbMoreContent.newBuilder();
						int iconId = more.getIntIconId();
						AppIconTab appIconTab = appIconTabService.get(iconId);
						String iconUrl = "";
						if (appIconTab!=null) {
							iconUrl = appIconTab.getVarIconUrl();
						}
						
						pbMoreB.setIntAppId(more.getIntAppId());
						pbMoreB.setIntMoreActionType(more.getIntMoreActionType());
						pbMoreB.setIntMoreContentId(more.getIntMoreContent());						
						pbMoreB.setVarMoreName(more.getVarMoreName());
						pbMoreB.setVarMoreContent(more.getVarMoreContent());
						pbMoreB.setVarMoreImgUrl(iconUrl);
						pbMoreB.setIntOrder(more.getIntOrder());
						
						pbMoreListB.addPbMoreContentList(pbMoreB);
					}
					AppPageViewTab view = appPageViewTabService.get(moreContList.get(0).getIntViewId());
					pbMoreListSCB.setIntModuleStyleId(view.getIntModuleStyleId());
					pbMoreListSCB.setPbMoreContentList(pbMoreListB);
					pbMoreListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbMoreListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbMoreListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbMoreListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbMoreListSCB.build().toByteArray();
	}

	/**
	 * 获得推送的消息列表 GET_PUSH_MESSAGELIST
	 * 
	 * @param pushMessageListCS
	 * @return
	 * @author 甘建新 2012-7-11下午11:47:41
	 */
	public byte[] getPushMessageList(byte[] bytes) {
		LOG_METHOD_NAME="getPushMessageList";
		
		PbPushMessageListSC.Builder pbPushMsgSCB = PbPushMessageListSC.newBuilder();
		PbPushMessageList.Builder pbPushMsgList = PbPushMessageList.newBuilder();
		if (bytes!=null) {
			try {
				PbPushMessageListCS pbPushMsgCS = PbPushMessageListCS.parseFrom(bytes);
				int appId = pbPushMsgCS.getIntAppId();
				int pageNum = pbPushMsgCS.getIntPageNum();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",pageNum:+"+pageNum+"】");
				
				int pageCount = pushMessageTabService.getPushMessagePageCount(appId);
				pbPushMsgSCB.setIntPageCount(pageCount);
				
				//查询数据
				List<PushMessageTab> pushMsgList = pushMessageTabService.getPushMessageListByPageNum(appId, pageNum);
				if (pushMsgList!=null && pushMsgList.size()>0) {
					for (PushMessageTab pushMsg : pushMsgList) {
						PbPushMessage.Builder pushMsgB = PbPushMessage.newBuilder();
						pushMsgB.setIntAppId(pushMsg.getIntAppId());
						pushMsgB.setIntPushMsgId(pushMsg.getIntPushMsgId());
						pushMsgB.setIntPushMsgStatus(pushMsg.getIntPushMsgStatue());
						pushMsgB.setIntPushMsgType(pushMsg.getIntPushMsgType());
						
						pushMsgB.setVarPushMsgContent(pushMsg.getVarPushMsgContent());
						pushMsgB.setVarPushMsgDescrip(pushMsg.getVarPushMsgDescrip());
						pushMsgB.setVarPushMsgName(pushMsg.getVarPushMsgName());
						
						pushMsgB.setDatPushMsgDatetime(pushMsg.getDatDatPushMsgDatetime());
						pushMsgB.setDatPushMsgEndDatetime(pushMsg.getDatPushMsgEndDatetime());
						
						pbPushMsgList.addPbPushMessageList(pushMsgB);
					}
					pbPushMsgSCB.setPbPushMessageList(pbPushMsgList);
					pbPushMsgSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbPushMsgSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbPushMsgSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbPushMsgSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbPushMsgSCB.build().toByteArray();
	}
	
	/**
	 * 获得需要数据更新的接口
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-19下午10:00:28
	 */
	public byte[] getDataUpdateInterface(byte[] bytes){
		LOG_METHOD_NAME="getDataUpdateInterface";
		
		PbDataUpdateSC.Builder pbDataUSCB = PbDataUpdateSC.newBuilder();
		PbInterfaceUpdateList.Builder pbIntUpdateListB = PbInterfaceUpdateList.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbDataUpdateCS pbDataUCS = PbDataUpdateCS.parseFrom(bytes);
				int appId = pbDataUCS.getIntAppId();
				log.info(LOG_METHOD_NAME+"【appId:"+appId+"】");
				//执行查询
				List<DataInterfaceUpdateTab> dataInterUpdateList = dataInterfaceUpdateTabService.getUpdateInterfaceList(appId);
				if (dataInterUpdateList!=null && dataInterUpdateList.size()>0) {
					for (DataInterfaceUpdateTab data : dataInterUpdateList) {
						PbInterfaceUpdate.Builder pbInterfUpdateB = PbInterfaceUpdate.newBuilder();
						pbInterfUpdateB.setIntAppId(data.getIntAppId());
						pbInterfUpdateB.setIntUpdateID(data.getIntUpdateId());
						pbInterfUpdateB.setIntUpdateVersion(data.getIntUpdateVersion());
						
						pbInterfUpdateB.setVarUpdateChnName(data.getVarUpdateChnName());
						pbInterfUpdateB.setVarUpdateCmd(data.getVarUpdateCommend());
						pbInterfUpdateB.setVarUpdateEngName(data.getVarUpdateEngName());
						
						pbIntUpdateListB.addPbInterfaceUpdate(pbInterfUpdateB);
					}
					pbDataUSCB.setPbInterfaceUpdateList(pbIntUpdateListB);
					pbDataUSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbDataUSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbDataUSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else{
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbDataUSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbDataUSCB.build().toByteArray();
	}
	
	/**
	 * 更新应用Loading图片
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-19下午10:00:43
	 */
	public byte[] getUpdateAppLoadingImg(byte[] bytes){
		LOG_METHOD_NAME="getUpdateAppLoadingImg";
		
		PbUpdateAppLoadingImgSC.Builder pbUpdateAppLISCB = PbUpdateAppLoadingImgSC.newBuilder();
		PbAppLoadingImgList.Builder pbAppLoadingImgListB = PbAppLoadingImgList.newBuilder();
		if (bytes!=null) {
			try {
				//参数转换
				PbUpdateAppLoadingImgCS pbUpdateAppLICS = PbUpdateAppLoadingImgCS.parseFrom(bytes);
				int appId = pbUpdateAppLICS.getAppId();
				int updateAppId = pbUpdateAppLICS.getAppUpdateId();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",updateAppId:"+updateAppId+"】");
				
				List<AppLoadingUpdateTab> appLoadingList = appLoadingUpdateTabService.getAppLoadingList(updateAppId);
				if (appLoadingList!=null && appLoadingList.size()>0) {
					for (AppLoadingUpdateTab appLoading : appLoadingList) {
						PbAppLoadingImg.Builder pbAppLoadingImgB = PbAppLoadingImg.newBuilder();
						pbAppLoadingImgB.setIntAppUpdateId(appLoading.getIntAppUpdateId());
						pbAppLoadingImgB.setIntIconUpdateId(appLoading.getIntIconUpdateId());
						
						//获得Loading图片的部分信息
						AppIconTab appIconTab = appIconTabService.get(appLoading.getIntIconId());
						String iconName = "";
						String downloadUrl = "";
						if (appIconTab!=null) {
							iconName = appIconTab.getIntIconName();
							downloadUrl = appIconTab.getVarIconUrl();
						}
						
						pbAppLoadingImgB.setVarIconName(iconName);
						pbAppLoadingImgB.setVarImgDownloadURL(downloadUrl);
						
						pbAppLoadingImgB.setDatIconDownloadDatetime(appLoading.getDatIconDownloadDatetime());
						pbAppLoadingImgB.setDatIconEndtime(appLoading.getDatIconEndtime());
						
						pbAppLoadingImgListB.addPbAppLoadingImg(pbAppLoadingImgB);
					}
					pbUpdateAppLISCB.setPbAppLoadingImgList(pbAppLoadingImgListB);
					pbUpdateAppLISCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else{
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbUpdateAppLISCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbUpdateAppLISCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbUpdateAppLISCB.setExeResult(PBRESULT.FAILED);
		}
		return pbUpdateAppLISCB.build().toByteArray();
	}
	
	/**
	 * 参加活动
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-8-9下午04:08:07
	 */
	public byte[] joinActive(byte[] bytes){
		LOG_METHOD_NAME="joinActive";
		
		PbJoinActiveSC.Builder pbJoinActSCB = PbJoinActiveSC.newBuilder();
		if (bytes!=null && bytes.length>0) {
			try {
				PbJoinActiveCS pbJoinActCS = PbJoinActiveCS.parseFrom(bytes);
				int userId = pbJoinActCS.getIntUserId();
				int contentId = pbJoinActCS.getIntContentId();
				
				log.info(LOG_METHOD_NAME+"【userId:"+userId+",contentId:"+contentId+"】");
				
				String joinDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				if (userId>0 && contentId>0) {
					ActiveUserTab activeUserTab = new ActiveUserTab();
					activeUserTab.setIntUserId(userId);
					activeUserTab.setIntContentId(contentId);
					activeUserTab.setDatJoinActiveDatetime(joinDate);
					activeUserTabService.save(activeUserTab);
					
					pbJoinActSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+"：参数不满足条件");
					pbJoinActSCB.setExeResult(PBRESULT.FAILED);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbJoinActSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbJoinActSCB.setExeResult(PBRESULT.OTHER);
		}
		return pbJoinActSCB.build().toByteArray();
	}
	

	// Setter
	public void setFeedbackTabService(FeedbackTabService feedbackTabService) {
		this.feedbackTabService = feedbackTabService;
	}

	public void setClientUserTabService(ClientUserTabService clientUserTabService) {
		this.clientUserTabService = clientUserTabService;
	}

	public void setFavoritesTabService(FavoritesTabService favoritesTabService) {
		this.favoritesTabService = favoritesTabService;
	}

	public void setShareTabService(ShareTabService shareTabService) {
		this.shareTabService = shareTabService;
	}

	public void setCommentTabService(CommentTabService commentTabService) {
		this.commentTabService = commentTabService;
	}

	public void setOrderTabService(OrderTabService orderTabService) {
		this.orderTabService = orderTabService;
	}

	public void setMoreContentTabService(MoreContentTabService moreContentTabService) {
		this.moreContentTabService = moreContentTabService;
	}

	public void setPushMessageTabService(PushMessageTabService pushMessageTabService) {
		this.pushMessageTabService = pushMessageTabService;
	}

	public void setDataInterfaceUpdateTabService(
			DataInterfaceUpdateTabService dataInterfaceUpdateTabService) {
		this.dataInterfaceUpdateTabService = dataInterfaceUpdateTabService;
	}

	public void setAppLoadingUpdateTabService(
			AppLoadingUpdateTabService appLoadingUpdateTabService) {
		this.appLoadingUpdateTabService = appLoadingUpdateTabService;
	}

	public void setAppIconTabService(AppIconTabService appIconTabService) {
		this.appIconTabService = appIconTabService;
	}

	public void setActiveUserTabService(ActiveUserTabService activeUserTabService) {
		this.activeUserTabService = activeUserTabService;
	}

	public void setAppPageViewTabService(AppPageViewTabService appPageViewTabService) {
		this.appPageViewTabService = appPageViewTabService;
	}

	public void setManageUserTabService(ManageUserTabService manageUserTabService) {
		this.manageUserTabService = manageUserTabService;
	}
}
